package com.gitee.wsl.android.drawable

import android.graphics.Canvas
import android.graphics.ColorFilter
import android.graphics.Rect
import android.graphics.drawable.Drawable


class DelegateDrawable(private var mCurrentDelegate: Drawable) : Drawable(), Drawable.Callback {

    override fun draw(canvas: Canvas) {
        mCurrentDelegate.draw(canvas)
    }

    override fun setAlpha(alpha: Int) {
        mCurrentDelegate.alpha = alpha
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        mCurrentDelegate.colorFilter = colorFilter
    }

    override fun setVisible(visible: Boolean, restart: Boolean): Boolean {
        super.setVisible(visible, restart)
        return mCurrentDelegate.setVisible(visible, restart)
    }

    override fun onBoundsChange(bounds: Rect) {
        mCurrentDelegate.bounds = bounds
    }

    override fun isStateful(): Boolean {
        return mCurrentDelegate.isStateful
    }

    override fun onStateChange(state: IntArray): Boolean {
        return mCurrentDelegate.setState(state)
    }

    override fun onLevelChange(level: Int): Boolean {
        return mCurrentDelegate.setLevel(level)
    }

    @Deprecated("Deprecated in Java")
    override fun getOpacity(): Int {
        return mCurrentDelegate.opacity
    }

    fun setCurrent(newDelegate: Drawable): Drawable {
        val previousDelegate = setCurrentWithoutInvalidate(newDelegate)
        invalidateSelf()
        return previousDelegate
    }

    private fun setCurrentWithoutInvalidate(newDelegate: Drawable): Drawable {
        val previousDelegate = mCurrentDelegate
        copyProperties(newDelegate, previousDelegate)
        mCurrentDelegate = newDelegate
        return previousDelegate
    }

    fun refreshCurrent(newDelegate: Drawable) {
        if (newDelegate == mCurrentDelegate) {
            return
        }
        val previousDelegate = mCurrentDelegate
        copyProperties(previousDelegate, newDelegate)
        mCurrentDelegate = newDelegate
        invalidateSelf()
    }

    override fun getIntrinsicWidth(): Int {
        return mCurrentDelegate.intrinsicWidth
    }

    override fun getIntrinsicHeight(): Int {
        return mCurrentDelegate.intrinsicHeight
    }

    override fun getPadding(padding: Rect): Boolean {
        return mCurrentDelegate.getPadding(padding)
    }

    override fun mutate(): Drawable {
        mCurrentDelegate.mutate()
        return this
    }

    override fun getCurrent(): Drawable {
        return mCurrentDelegate
    }

    override fun invalidateDrawable(who: Drawable) {
        invalidateSelf()
    }

    override fun scheduleDrawable(who: Drawable, what: Runnable, `when`: Long) {
        scheduleSelf(what, `when`)
    }

    override fun unscheduleDrawable(who: Drawable, what: Runnable) {
        unscheduleSelf(what)
    }

    companion object {
        private fun copyProperties(to: Drawable, from: Drawable) {
            if ( to == from) {
                return
            }
            to.bounds = from.bounds
            to.changingConfigurations = from.changingConfigurations
            to.level = from.level
            to.setVisible(from.isVisible, false)
            to.state = from.state
        }
    }
}